package cn.m3a1.control.teacher;

import cn.m3a1.bean.AbsentNote;
import cn.m3a1.bean.Curse;
import cn.m3a1.bean.Msg;
import cn.m3a1.bean.User;
import cn.m3a1.service.teacher.TeacherService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author wyj
 * @date 2019/7/18
 * @time 下午12:21
 */
@Controller
@RequestMapping("/teacher")
public class TeacherControl {
    @Autowired
    TeacherService teacherService;

    /**
     * 教师登录的首页，显示该教师所有课程的信息
     *
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping("/teaCouse")
    public String teaCourse(HttpServletRequest request, ModelMap modelMap) {
        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");
        List<Curse> curse = teacherService.getCurse(loginUser.getId());
        modelMap.put("members", curse);
        modelMap.put("loginUser", loginUser);
        return "teacher/teaCourse";
    }

    /**
     * 获取该班级的学生名单
     *
     * @param classid  班级id
     * @param modelMap
     * @return
     */
    @RequestMapping("/getStudents")
    public String getStudents(int classid, ModelMap modelMap) {
        List<User> userByTclass = teacherService.getUserByTclass(classid);
        modelMap.put("members", userByTclass);
        return "/teacher/students";
    }
@RequestMapping("/teaLeave")
    public String teaLeave(ModelMap modelMap, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");
        List<AbsentNote> leave = teacherService.getLeave(loginUser.getId());
        System.out.println(loginUser.getId());
        modelMap.put("loginUser",loginUser);
        modelMap.put("absentnote", leave);
        return "/teacher/teaLeave";

    }

    /**
     * 教师审批
     * @param absentNote
     * @return
     */
    @RequestMapping("/setLeave")
    @ResponseBody
    public Msg setLeave(HttpServletRequest request,AbsentNote absentNote){
        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");
        absentNote.setTeacherid(loginUser.getId());
        AbsentNote absent = teacherService.getAbsent(absentNote.getId());
        if (absent==null|| !absent.getTeacherid().equals(loginUser.getId())){
            return  Msg.fail().add("msg","假条信息不对或者教师信息不符");
        }
        absent.setTeachercfm(absentNote.getTeachercfm());
        absent.setTeachermark(absentNote.getTeachermark());
        absent.setStatus(2);
        boolean b = teacherService.setLeave(absent);
            if (b){
                return Msg.success();
            }else{
                return  Msg.fail();
            }
    }

}
